#include "MySplashScreen.h"
#include "MyEngine.h"
#include "MyPlayer.h"
#include "MyPlayerActionManager.h"

MySplashScreen::MySplashScreen(MyGame* _this)
{
	parent = _this;
	load_imgs();
	init();
	prepare_form();
	popup_visible = 0;
}
MySplashScreen::~MySplashScreen()
{
 	unload_surface(formSheet);
	unload_surface(formSheetBkp);
	unload_surface(itemSheet);
	unload_surface(popupSheetBkp);
	unload_surface(popupSheet);
	for (int i = 0; i < NUM_OF_ITEMS; i++) delete(trade_items_array[i]);
}
void MySplashScreen::prepare_form()
{
	//clean form
	SDL_BlitSurface(formSheetBkp, 0, formSheet, 0);

	//add trade items
	for (int i = 0; i < NUM_OF_ITEMS; i++)
		SDL_BlitSurface(itemSheet, trade_items_array[i]->sheet_clip, formSheet, trade_items_array[i]->screen_clip);

	//add gold
	char * tmpchar = new char[10];
	sprintf(tmpchar, "%d gp", parent->actPlayer->get_money());
	SDL_Surface *tmp = TTF_RenderText_Blended (parent->parent->font, tmpchar, *(parent->color_exp1));
	SDL_Rect *tmp1;
	SDL_BlitSurface(tmp, 0, formSheet, tmp1 = make_rect(180, 560, tmp->w, tmp->h));
	delete(tmp1);
	unload_surface(tmp);
}
void MySplashScreen::load_imgs()
{
	#ifdef _WIN32
	formSheet		= load_image("misc\\splash_form.jpg");
	formSheetBkp	= load_image("misc\\splash_form.jpg");
	itemSheet		= load_image("misc\\trade_items.jpg");
	popupSheetBkp	= load_image("misc\\popup.png");
	popupSheet		= load_image("misc\\popup.png");
	#else 
	formSheet		= load_image("misc/splash_form.jpg");
	formSheetBkp	= load_image("misc/splash_form.jpg");
	itemSheet		= load_image("misc/trade_items.jpg");
	popupSheetBkp	= load_image("misc/popup.png");
	popupSheet		= load_image("misc/popup.png");
	#endif
	
	if ((!formSheet)||(!itemSheet)||(!popupSheet))
	{	
		printf("ERROR: Cannot load game images!\n ('splash_form.jpg', 'trade_items.jpg' and/or 'popup.png' missing?");
		parent->demand_quit();
	}    		
}
void MySplashScreen::init()
{
	int prices[NUM_OF_ITEMS] = {50,150,300, 200, 500, 150};
	char * descriptions[NUM_OF_ITEMS] = {"Book of Fireball", "Book of Frostball", "Book of Lightning", "Fire Arrow Bow", "Ice Arraw Bow", "Mysterious Boost"};
	for (int i = 0; i < NUM_OF_ITEMS; i++)
	{
		trade_items_array[i] = new tradeItem(descriptions[i], prices[i], make_rect(25 + (i%3)*200, i>=3 ? 350 : 150, 150, 150), make_rect(0, i*150, 150, 150));
	}
}
void MySplashScreen::displaySplashScreen(int _t)
{
	SDL_Rect* tmp;
	SDL_BlitSurface(formSheet, 0,	parent->parent->screen, tmp=make_rect(100,0,formSheet->w, formSheet->h));
	delete(tmp);

	switch(popup_visible)
	{
		case 1:
			{
				//popup emerging
				time_current += _t;					 
				if (time_current > time_final) 	popup_visible = 2;
				if (time_current/time_final < 0.3) time_current += 200;
				else if (time_current/time_final < 0.6) time_current += 80;
				else if (time_current/time_final < 0.9) time_current += 20;
				int y = int(200*time_current/time_final);
				SDL_BlitSurface(popupSheet, 0,	parent->parent->screen, tmp=make_rect(345, y, popupSheet->w, popupSheet->h));
				break;
			}
		case 2:
			{
				SDL_BlitSurface(popupSheet, 0,	parent->parent->screen, tmp=make_rect(parent->parent->screen->w-popupSheet->w, 400-popupSheet->w/2, popupSheet->w, popupSheet->h));
				break;
			}
		case 3:
			{
				//popup fading
				time_current += _t;					 
				if (time_current > time_final) 
				{
					popup_visible = 0;
					return;
				}
				int y = 200+int(400*time_current/time_final);
				SDL_BlitSurface(popupSheet, 0,	parent->parent->screen, tmp=make_rect(345, y, popupSheet->w, popupSheet->h));
			}
	}
}
bool MySplashScreen::process_click(int _x, int _y)
{
	switch(popup_visible)
	{
	case 0:
	{
		//check trade items
		for (int i = 0; i < NUM_OF_ITEMS; i++)
		{
			//click within the trade item button 
			if ((_x > (trade_items_array[i]->screen_clip->x+100)) && 
				(_x < (trade_items_array[i]->screen_clip->x+100+trade_items_array[i]->screen_clip->w)) && 
				(_y > trade_items_array[i]->screen_clip->y) &&  
				(_y < trade_items_array[i]->screen_clip->y+trade_items_array[i]->screen_clip->h)) 
					{
						selected_item = i;
						create_popup(i,0);
						popup_visible = 1;
					}
		}
		if ((_x > 547)&&(_x < 676)&&(_y > 548)&&(_y < 576)) return finised();		
		break;
	}
	case 1:
		break;
	case 2:
	{
		//fade the message
		if (selected_item == -1) 
		{
			time_final = 200;
			time_current = 0;
			popup_visible = 3;
			return false;
		}

		//items bought
		if ((_x > 564)&&(_x < 624)&&(_y > 346)&&(_y < 358))
		{
			/*
			time_final = 200;
			time_current = 0;
			popup_visible = 3;*/

			switch(selected_item)
			{
			case 0:   
				return process_buy_clicked(ST_FIREBALL);
			case 1: 
				return process_buy_clicked(ST_FROSTBALL);
			case 2: 
				return process_buy_clicked(ST_LIGHTBALL);
			case 3: 
				return process_buy_clicked(ST_FIRE_BOW);
			case 4: 
				return process_buy_clicked(ST_ICE_BOW);
			case 5: 
				if (this->player_can_afford(selected_item))
				{
					parent->actPlayer->set_money_d(-trade_items_array[5]->price);
					create_popup(selected_item*10 + parent->actPlayer->get_random_boost(), 1);
				}
				else create_popup(selected_item, 2);	
			}
		}
		//deal canceled, fade popup
		if ((_x > 716)&&(_x < 766)&&(_y > 346)&&(_y < 358))
		{
			time_final = 200;
			time_current = 0;
			popup_visible = 3;
		}
	} //case 2
	} //switch
	return false;
}
bool MySplashScreen::process_buy_clicked(int item_pos_in_action_manager)
{
	if (player_can_afford(selected_item))
	{
		//if player already has this item
		if (parent->actPlayerActionManager->enableItem(item_pos_in_action_manager))
		{	
			create_popup(selected_item, 3);
		}
		//buy it
		else
		{
			parent->actPlayer->set_money_d(-trade_items_array[selected_item]->price);
			this->create_popup(selected_item, 1);
		}
	}
	else create_popup(selected_item, 2); 
	return false;
}
void MySplashScreen::create_popup(int i, int _option)
{
	SDL_Surface *tmp;
	SDL_Rect *tmp1;
	SDL_Color * thisTextColor = parent->color_exp1;
	//always start with a blank surface - popupSheet msg
	SDL_BlitSurface(popupSheetBkp, 0, popupSheet, 0);

	switch(_option) //option determines what text/img to add
	{
	case 0:	//just want to buy item
		{
		//add image to the msg
		SDL_BlitSurface(itemSheet, trade_items_array[i]->sheet_clip, popupSheet, 
			tmp1 = make_rect(40, 38, trade_items_array[i]->sheet_clip->w, trade_items_array[i]->sheet_clip->h));
		delete(tmp1);
		//add 1. line of text
		tmp = TTF_RenderText_Blended (parent->parent->font, "An old man tells you:", *(thisTextColor));
		SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,35,tmp->w,tmp->h));
		unload_surface(tmp);
		delete(tmp1);
		//add 2. line of text
		tmp = TTF_RenderText_Blended (parent->parent->font, "I'm willing to sell this precious", *(thisTextColor));
		SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,65,tmp->w,tmp->h));
		unload_surface(tmp);
		delete(tmp1);
		//add 3. line of text
		char * tmpchar = new char[100];
		sprintf(tmpchar, "%s for mere %d gp!", trade_items_array[i]->description, trade_items_array[i]->price);
		tmp = TTF_RenderText_Blended (parent->parent->font, tmpchar, *(thisTextColor));
		SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,85,tmp->w,tmp->h));
		unload_surface(tmp);
		delete(tmp1);
		//add 3. line of text
		tmp = TTF_RenderText_Blended (parent->parent->font, "Do we have a deal?", *(thisTextColor));
		SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-(tmp->w/2),140,tmp->w,tmp->h));
		unload_surface(tmp);
		delete(tmp1);
		//add options line of text (4.)
		tmp = TTF_RenderText_Blended (parent->parent->font, "Sure, man!             No way!", *(thisTextColor));
		SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-(tmp->w/2),170,tmp->w,tmp->h));
		unload_surface(tmp);
		delete(tmp1);
		//emerge from the top
		popup_visible = 1;
		time_final = 2000;
		time_current = 0;
		break;
		}
	case 1:{ //transfer successful
			int j = 0;	
			if (i > 10) //mysterious gift
			{
				j = i % 10;		//gift received
				i /= 10;		
			}
			SDL_BlitSurface(itemSheet, trade_items_array[i]->sheet_clip, popupSheet, 
				tmp1 = make_rect(40, 38, trade_items_array[i]->sheet_clip->w, trade_items_array[i]->sheet_clip->h));
			delete(tmp1);
			//add 1. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, "After shaking hands with the old man,", *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,50,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);
			//add 2. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, "you inspect your brand new", *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,70,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);
			//add 3. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, trade_items_array[i]->description, *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,90,tmp->w,tmp->h));
			unload_surface(tmp);
			//add 4. line of text
			delete(tmp1);
			tmp = TTF_RenderText_Blended (parent->parent->font, "with a loving look. Cool!", *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,110,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);

			switch (j)
			{
				case 0: 
					break;
				case 1:
				{
					tmp = TTF_RenderText_Blended (parent->parent->font, "You feel more experienced!", *(thisTextColor));
					SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,130,tmp->w,tmp->h));
					unload_surface(tmp);
					delete(tmp1);
					break;
				}
				case 2:
				{
					tmp = TTF_RenderText_Blended (parent->parent->font, "You feel more healty!", *(thisTextColor));
					SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,130,tmp->w,tmp->h));
					unload_surface(tmp);
					delete(tmp1);
					break;
				}
				case 3:
				{
					tmp = TTF_RenderText_Blended (parent->parent->font, "You feel more intelligent!", *(thisTextColor));
					SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,130,tmp->w,tmp->h));
					unload_surface(tmp);
					delete(tmp1);
					break;
				}
				case 4:
				{
					tmp = TTF_RenderText_Blended (parent->parent->font, "You are so lucky! You found a bag of gold!", *(thisTextColor));
					SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,130,tmp->w,tmp->h));
					unload_surface(tmp);
					delete(tmp1);
					break;
				}
				case 5:
				{
					tmp = TTF_RenderText_Blended (parent->parent->font, "You don't feel anything special...", *(thisTextColor));
					SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,130,tmp->w,tmp->h));
					unload_surface(tmp);
					delete(tmp1);
					break;
				}
			}
			//actualize gold
			prepare_form();
			selected_item = -1;
			popup_visible = 2;
			break;
		}
	case 2: //not enough money
		{
			//add 1. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, "Old man looks... unhappy:", *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(232-tmp->w/2,70,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);
			//add 2. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, "\"Ya silly crackhead! Ya have no money!\"" , *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(232-tmp->w/2,100,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);
			//add 3. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, "\"Now get lost before I go medieval on your A$$!\"" , *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(232-tmp->w/2,130,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);

			selected_item = -1;
			popup_visible = 2;
			break;
		}
	case 3: //already haved this item
		{
			SDL_BlitSurface(itemSheet, trade_items_array[i]->sheet_clip, popupSheet, 
				tmp1 = make_rect(40, 38, trade_items_array[i]->sheet_clip->w, trade_items_array[i]->sheet_clip->h));
			delete(tmp1);
			//add 1. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, "Old man smiles at you:", *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,50,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);
			//add 2. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font, "\"Ya know, I appreciate your interest,", *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,70,tmp->w,tmp->h));
			unload_surface(tmp);
			delete(tmp1);
			//add 3. line of text
			tmp = TTF_RenderText_Blended (parent->parent->font,"but you already have one of these\"", *(thisTextColor));
			SDL_BlitSurface(tmp, 0, popupSheet, tmp1 = make_rect(320-tmp->w/2,90,tmp->w,tmp->h));
			unload_surface(tmp);
			selected_item = -1;
			popup_visible = 2;
			break;
		}
	}
}
bool MySplashScreen::finised(){return true;}
bool MySplashScreen::player_can_afford(int i)
{
	return trade_items_array[i]->price <= parent->actPlayer->get_money();
}